﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections;


namespace WpfApplicationWithGrid
{
    class jsonConverter
    {
        private static jsonConverter converter = null;
        private static Hashtable method_table = new Hashtable();
        
        private jsonConverter() {

            method_table.Add("FIFO", "First In First Out");
            method_table.Add("LRU", "Least Recently Used");
            method_table.Add("LFU", "Least Frequently Used");
        }
        public static jsonConverter getConverter()
        {
            if (converter == null) converter = new jsonConverter();
            return converter;
        }

        /**
         * 
         * */
        public void setJsonToObject(string json, char which)
        {
            
            JObject update = JObject.Parse(json);


            if(which == 'p'){

                Parameter current = Parameter.getParameter();
                List<int> temp = new List<int>();

                current.CacheSize = int.Parse(update["cachesize"].ToString());
                current.CacheMethod = (string) method_table[(string)update["cachemethod"]];
                current.InputStream = strArrayToArrayList((JArray)update["inputstream"]);
                current.UpdateFreq = int.Parse(update["updatefreq"].ToString());
                current.Hist = (int.Parse(update["his"].ToString()));

                current.Stock_25 = (int.Parse(update["stock_25"].ToString()));
                current.Realtime = (int.Parse(update["realtime"].ToString()));
                
                current.TimeDuration = int.Parse(update["trackduration"].ToString());
            }
            else if (which == 'w')
            {
                WorkFlow current = WorkFlow.getWorkFlow();

                current.HitRatio = float.Parse(update["hitratio"].ToString());
                current.SucessRatio = float.Parse(update["successratio"].ToString());
                current.AvgTime = float.Parse(update["avgprocesstime"].ToString()) / 1000;
                current.MobileUser = int.Parse(update["mobile"].ToString());
                current.WebUser = int.Parse(update["web"].ToString());
                current.Broker = int.Parse(update["broker"].ToString());

                
                DataVisualize.AddAvgRatio(current.AvgTime);
                DataVisualize.AddHitRatio(current.HitRatio);
                DataVisualize.AddSuccessRatio(current.SucessRatio);

                DataVisualize.dataUpdate();


            }
        }
       
        public bool[] jArrayToBoolArray(JArray target)
        {
            bool[] result = new bool[target.Count];
            for (int i = 0; i < target.Count; i++)
            {
                result[i] = (bool) target[i];
            }
            return result;
        }
        public ArrayList strArrayToArrayList(JArray target)
        {
            ArrayList update = new ArrayList();
            foreach (JToken item in target)
            {
                update.Add(item);
            }
            return update;
        }
        public string setParameterToJson()
        {
            Parameter current = Parameter.getParameter();
            string update = JsonConvert.SerializeObject(current);

            return update;
            
            //return the update back to team b
        }
    }
}
